package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageBean;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date end){
        log.info("员工查询 参数 : page={},pageSize={},name={},gender={},begin={},end={}"
                ,page,pageSize,name,gender,begin,end);
        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delList(@PathVariable List<Long> ids){
        log.info("员工的批量删除:{}",ids);

        empService.delList(ids);
        //响应
        return Result.success();
    }
    @PostMapping
    public  Result add(@RequestBody Emp emp){
        log.info("新增员工,{}",emp);
        empService.add(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
       Emp emp =  empService.getById(id);
       return Result.success(emp);
    }
    @PutMapping
    public Result edit(@RequestBody Emp emp){
        empService.edit(emp);
        return Result.success();
    }

}
